import useStorage from '@/composables/useStorage.ts'
import {RouteLocationNormalized, Router, NavigationGuardNext,} from 'vue-router'
import {useThemeConfig} from '@/stores/themeConfig.ts'
import {pinia} from '@/stores'

export default (router: Router) => {
    router.beforeEach(beforeEach)
}

//路由守卫
async function beforeEach(to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) {
    const {setHistoryRoute} = useThemeConfig(pinia)
    const token = useStorage().get('token')
    if (!token && to.name === 'login') {
        next()
    } else {
        if (!token) {
            next({
                name: 'login'
            })
        } else if (token && to.name === 'login') {
            next({
                name: 'home'
            })
        } else {
            setHistoryRoute(to)
            next()
        }
    }
}



